package coml.jiyun.hp.day09work;

import android.Manifest;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 读取联系人
     */
    private Button mBtn;
    private LinearLayout mActivityMain;
    /**
     * 读取联系人
     */
    private Button mBtnReadPreson;
    /**
     * 读取音频
     */
    private Button mBtnReadMusic;
    /**
     * 读取视频
     */
    private Button mBtnReadVideo;
    private ContentResolver contentResolver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        String[] pers = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_PHONE_STATE
        };
    }

    private void initView() {
        mBtn = (Button) findViewById(R.id.btn_read_preson);
        mBtn.setOnClickListener(this);
        mActivityMain = (LinearLayout) findViewById(R.id.activity_main);
        mBtnReadPreson = (Button) findViewById(R.id.btn_read_preson);
        mBtnReadPreson.setOnClickListener(this);
        mBtnReadMusic = (Button) findViewById(R.id.btn_read_music);
        mBtnReadMusic.setOnClickListener(this);
        mBtnReadVideo = (Button) findViewById(R.id.btn_read_video);
        mBtnReadVideo.setOnClickListener(this);
        mActivityMain.setOnClickListener(this);

        contentResolver = getContentResolver();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_read_preson:
                ReadPerson();
                break;
            case R.id.btn_read_music:
                ReadMusic();
                break;
            case R.id.btn_read_video:
                ReadVideo();
                break;
        }
    }

    private void ReadVideo() {
        Cursor query = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,
                null, null);
        String path = "";
        while (query.moveToNext()) {

            // 图片的名称
            String name = query.getString(query.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
            path = query.getString(query.getColumnIndex(MediaStore.Video.Media.DATA));
            Log.i("tag", "视频的名称：" + name);
        }
    }

    private void ReadMusic() {
        Cursor query = contentResolver.query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, null, null, null, null);
        while (query.moveToNext()) {
            String string = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));
            Log.d("TAG", "音频名称: " + string);
        }
    }

    private void ReadPerson() {
        ContentResolver resolver = getContentResolver();
        // content://com.android.contacts/raw_contacts为联系人内容提供者的uri
        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        // content://com.android.contacts/data为联系人数据库的内容提供者的uri
        Uri dataUri = Uri.parse("content://com.android.contacts/data");
        Cursor cursor = resolver.query(uri, null, null, null, null);
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("contact_id"));
            System.out.println(id);
            Cursor dataCursor = resolver.query(dataUri, null, "raw_contact_id=?", new String[]{id}, null);
            while (dataCursor.moveToNext()) {
                String data1 = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                String mimetype = dataCursor.getString(dataCursor.getColumnIndex("mimetype"));
                System.out.println("data1=" + data1 + "----" + "mimetype=" + mimetype);
            }
            dataCursor.close();
        }
        cursor.close();
    }
}